/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.GroupRelationship;

/**
 * An instance of GroupRelationshipDAO is a data access object used to access
 * GroupRelationship, which contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class GroupRelationshipDAO extends BaseDAO {

	public List<GroupRelationship> findByProperty(String propertyName,
			Object value, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from GroupRelationship as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<GroupRelationship> results = queryObject.list();

		return results;

	}

	public boolean exists(int gid1, int gid2) {
		List<GroupRelationship> result = findBy2Property("gid1", new Integer(
				gid1), "gid2", new Integer(gid2));
		if (result != null && result.size() > 0)
			return true;

		return false;
	}

	public List<GroupRelationship> findByProperty(String propertyName,
			Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from GroupRelationship as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		@SuppressWarnings("unchecked")
		List<GroupRelationship> results = queryObject.list();

		return results;

	}

	public List<GroupRelationship> findBy2Properties(String propertyName,
			Object value, String propertyName2, Object value2, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from GroupRelationship as model where model."
				+ propertyName + "= ? and " + propertyName2 + "= ?  ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, value2);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<GroupRelationship> results = queryObject.list();

		return results;

	}

	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from GroupRelationship as model where model."
				+ propertyName + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public List<GroupRelationship> findBy2Property(String propertyName1,
			Object value1, String propertyName2, Object value2) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from GroupRelationship as model where model."
				+ propertyName1 + "= ? and model." + propertyName2 + "= ? ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value1);
		queryObject.setParameter(1, value2);
		@SuppressWarnings("unchecked")
		List<GroupRelationship> results = queryObject.list();

		return results;

	}

	public List<GroupRelationship> findAll(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from GroupRelationship";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<GroupRelationship> results = queryObject.list();

		return results;

	}

}